package com.xzh.mallorderapi.api;

import com.mall.common.util.JsonResult;
import com.xzh.mallorderspi.model.OrderItem;
import com.xzh.mallorderspi.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "api/orderItems",produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderItemApi {
    private OrderItemService orderItemService;

    @Autowired
    public void setOrderItemService(OrderItemService orderItemService) {
        this.orderItemService = orderItemService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(OrderItem  search){
        List<OrderItem> list = orderItemService.findAll(search);
        return ResponseEntity.ok(JsonResult.success(list));

    }

    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody OrderItem orderItem){
        boolean result = orderItemService.save(orderItem);
        if(result){
            return ResponseEntity.ok(JsonResult.success(result));
        }else {
            return ResponseEntity.ok(JsonResult.fail("订单项保存失败"));
        }
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody OrderItem orderItem){
        boolean result = orderItemService.update(orderItem);
        if(result){
            return ResponseEntity.ok(JsonResult.success(result));
        }else {
            return ResponseEntity.ok(JsonResult.fail("订单项更新失败"));
        }
    }

    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds(@RequestBody List<Integer> ids){
        int result = orderItemService.deleteByIds(ids);
        if(result > 0){
            return ResponseEntity.ok(JsonResult.success(result));
        }else {
            return ResponseEntity.ok(JsonResult.fail("订单项删除失败"));
        }
    }

    @GetMapping("deleteBy")
    public ResponseEntity<JsonResult<?>> deleteBy(OrderItem search){
        int result = orderItemService.deleteBy(search);
        if(result > 0){
            return ResponseEntity.ok(JsonResult.success(result));
        }else {
            return ResponseEntity.ok(JsonResult.fail("订单项删除失败"));
        }
    }


}
